package com.punai.config.controller;

import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.config.constants.PnConfigConstants;
import com.punai.config.service.ConfigEngineerClassService;
import com.punai.config.service.ConfigFeatureElementService;
import com.punai.config.service.ConfigGenerateCodeService;
import com.punai.config.sqlitedomain.ConfigEngineerClass;
import com.punai.config.sqlitedomain.ConfigFeatureElement;
import com.punai.config.sqlitedomain.vo.ConfigFeatureElementVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/config/featureElement")
public class ConfigFeatureElementController extends BaseController {

    @Autowired
    private ConfigEngineerClassService configEngineerClassService;
    @Autowired
    private ConfigFeatureElementService configFeatureElementService;
    @Autowired
    private ConfigGenerateCodeService configGenerateCodeService;

    @PreAuthorize("@ss.hasPermi('config:featureElement:list')")
    @GetMapping("/treelist")
    public AjaxResult treelist() {
        List<ConfigEngineerClass> treelist = configEngineerClassService.treelist();
        return AjaxResult.success(treelist);
    }

    @PreAuthorize("@ss.hasPermi('config:featureElement:list')")
    @GetMapping("/list")
    public AjaxResult list(ConfigFeatureElementVo vo) throws ParseException {
        Page<ConfigFeatureElement> resultList = configFeatureElementService.selectList(vo);
        List<ConfigFeatureElement> content = resultList.getContent();
        return AjaxResult.success(content);
    }

    @PreAuthorize("@ss.hasPermi('config:featureElement:addedit')")
    @PostMapping
    public AjaxResult add(@RequestBody ConfigFeatureElement configFeatureElement) throws ParseException {
        return toAjax(configFeatureElementService.add(configFeatureElement));
    }

    @PreAuthorize("@ss.hasPermi('config:featureElement:query')")
    @GetMapping("/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable String id) throws ParseException {
        ConfigFeatureElement configFeatureElement = configFeatureElementService.getInfo(id);
        return AjaxResult.success(configFeatureElement);
    }

    @PreAuthorize("@ss.hasPermi('config:featureElement:remove')")
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable String id) {
        return toAjax(configFeatureElementService.deleteInfo(id));
    }

    @PreAuthorize("@ss.hasPermi('config:featureElement:query')")
    @GetMapping("checkCalculateCaliberType")
    public AjaxResult checkCalculateCaliberType(ConfigFeatureElement configFeatureElement) throws ParseException {
        String classId = configFeatureElement.getClassId();
        String calculateCaliberType = configFeatureElement.getCalculateCaliberType();
        boolean isExist = configFeatureElementService.checkCalculateCaliberType(classId,calculateCaliberType);
        return AjaxResult.success(isExist);
    }

    @PreAuthorize("@ss.hasPermi('config:featureElement:copy')")
    @PostMapping("/copyOtherClassNode/{classIds}/{elementIds}")
    public AjaxResult copyOtherClassNode(@PathVariable("classIds") String classIds,@PathVariable("elementIds") String elementIds) throws ParseException {
        return toAjax(configFeatureElementService.copyOtherClassNode(classIds,elementIds));
    }

    @PreAuthorize("@ss.hasPermi('config:featureElement:query')")
    @GetMapping("getNextCode")
    public AjaxResult getNextCode() {
        String nextCode = configGenerateCodeService.getNextCode(PnConfigConstants.CODE_TYPE.ELE_CODE);
        return AjaxResult.success("成功",nextCode);
    }

    @PreAuthorize("@ss.hasPermi('config:featureElement:syncCode')")
    @PutMapping("syncBasicCodeToDb")
    public AjaxResult syncBasicCodeToDb() {
        return toAjax(configGenerateCodeService.syncSqliteMaxCodeToDb(PnConfigConstants.CODE_TYPE.ELE_CODE));
    }

    @PreAuthorize("@ss.hasPermi('config:featureElement:query')")
    @PostMapping("/moveup/{elementIds}/{prevId}")
    public AjaxResult moveup(@PathVariable("elementIds") String elementIds,@PathVariable(value = "prevId",required = false) String prevId) throws ParseException {
        boolean s=configFeatureElementService.moveToFormatForm(elementIds,null,prevId);
        if (s){
            return AjaxResult.success("操作成功!");
        }else {
            return AjaxResult.error("操作失败!");
        }
    }

    @PreAuthorize("@ss.hasPermi('config:featureElement:query')")
    @PostMapping("/movedown/{elementIds}/{nextId}")
    public AjaxResult movedown(@PathVariable("elementIds") String elementIds,@PathVariable(value = "nextId",required = false) String nextId) throws ParseException {
        boolean s=configFeatureElementService.moveToFormatForm(elementIds,nextId,null);
        if (s){
            return AjaxResult.success("操作成功!");
        }else {
            return AjaxResult.error("操作失败!");
        }
    }
}
